package com.uusee.crawler.pageprocessor.video.sohu;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import net.sf.json.JSONObject;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.DomainUtils;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.sp.model.Ugc;
import com.uusee.util.StringUtils;

public class SohuVideoInfoInterfacePageProcessor extends Processor {
	private static Log log = LogFactory.getLog("SohuVideoInfoInterfacePageProcessor"); 
	private Lock lock = new ReentrantLock();
	private Map<String,String> channelMap = new HashMap<String,String>();
	private Map<String,String> categoryMap = new HashMap<String,String>();
	
	public void innerProcess(CrawlURI crawlURI) {
		if (!canProcess(crawlURI)) {
			return;
		}
		Date crawlDate = new Date();
		String videoSite = crawlURI.getSourceSite();
		String playUrl = crawlURI.getCrawlUrl();
		String crawlResult = crawlURI.getCrawlResult();
		
		Ugc ugc = (Ugc)crawlURI.getModel();
		if (ugc==null) {
			ugc = new Ugc();
		}
		String id = ugc.getVid();
		try {
			FastDateFormat fd0 = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
			
			JSONObject jsonObject = JSONObject.fromObject( crawlResult ); 
			int statusCode = jsonObject.getInt("status");
			if (statusCode!=1) {
				log.info(videoSite+"-"+id+"-数据不存在。");
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
				return;
			}
			
			JSONObject videoJsonObject = jsonObject.getJSONObject("data");
			
			String vid = videoJsonObject.getString("id");
			String evid = "";
			playUrl = "http://v.blog.sohu.com/u/vw/"+vid;
			String shareUrl = "http://v.blog.sohu.com/fo/v4/"+vid;
			String sourceSite = PageProcessorUtils.getValue("http://([^/]*?)/", playUrl);
			String domain = DomainUtils.getDomain(playUrl);
			String title = videoJsonObject.getString("title");
			String logo = videoJsonObject.getString("cusCoverURL");
			int length = videoJsonObject.getInt("videoLength");
			String channelId = videoJsonObject.getString("categoriesId");
			String channel = "";
			try {
				lock.lock();
				if (channelMap.containsKey(channelId)) {
					channel = channelMap.get(channelId);
				}
			}finally{
				lock.unlock();
			}
			String tags = videoJsonObject.getString("tag");
			String summary = videoJsonObject.getString("introduction");
			long fileSize = videoJsonObject.getLong("videoSize");
			
			int totalFav = videoJsonObject.getInt("videoFavorite_count");
			int totalComment = videoJsonObject.getInt("comments_count"); 
			
			String uploadUserid = videoJsonObject.getString("userId");
			String uploadUsername = videoJsonObject.getString("authorName");
			String uploadUserblog = "http://v.blog.sohu.com/u/us/"+uploadUserid;
			long uploadDateLong = videoJsonObject.getLong("uploadTime");
			String uploadDate = fd0.format(new Date(uploadDateLong));
			
			if (length==0 || fileSize==0 || StringUtils.isEmpty(logo) || StringUtils.isEmpty(title)) {
				log.info(videoSite+"-"+id+"-数据已经失效。");
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
				return;
			}
				
			ugc.setVideoSite("v.blog.sohu.com");
			ugc.setDomain(domain);
			ugc.setSourceSite(sourceSite);
			ugc.setPlayUrl(playUrl);
			ugc.setTitle(title);
			ugc.setLogo(logo);
			ugc.setLength(length);
			//ugc.setQuality(quality);
			ugc.setChannel(channel);
			//ugc.setCategory(categorys);
			ugc.setTags(tags);
			ugc.setSummary(summary);
			ugc.setVid(vid);
			ugc.setEvid(evid);
			ugc.setOriLogo(logo);
			ugc.setShareUrl(shareUrl);
			ugc.setTotalPageView(0);
			ugc.setTotalComment(totalComment);
			ugc.setTotalFav(totalFav);
			//ugc.setFileFormat(fileFormat);
			ugc.setFileSize(fileSize);
			ugc.setUploadUserid(uploadUserid);
			ugc.setUploadUsername(uploadUsername);	
			ugc.setUploadUserblog(uploadUserblog);
			ugc.setUploadDate(uploadDate);
			ugc.setLogoStatus("0");
			ugc.setStatus("1");
			ugc.setCrawlUser("uusee_crawler");
			ugc.setCrawlDate(crawlDate);
			ugc.setUpdateUser("uusee_crawler");
			ugc.setUpdateDate(crawlDate);
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(videoSite+"-"+id+"-处理时发生错误。",e);
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
		}
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}

	public void setChannelMap(Map<String, String> channelMap) {
		this.channelMap = channelMap;
	}
	
	
}
